package execute

import (
	"fucker/frame/context"
	"fyne.io/fyne/v2"
	"log"
	"reflect"
)

func ExecNative(methodName string, param []interface{}) []reflect.Value {
	method := context.Fc.GetMethod(methodName)

	if method.IsZero() {
		return nil
	}

	if len(param) == 0 {
		return method.Call(nil)
	}

	var paramValue []reflect.Value
	paramValue = append(paramValue, reflect.ValueOf(context.WContext))

	for _, v := range param {
		kind := reflect.TypeOf(v).Kind()
		switch kind {
		case reflect.String:
			s := v.(string)
			id := s[1:]
			//$1
			if len(s) > 2 && s[0:1] == "$" {

				log.Println("get widget id:" + id)
				str, err := context.WContext.GetStr(id)
				if err != nil {
					str = ""
				}
				v = str
				log.Println("get widget id:", id, "binding value:", v)
			} else {
				v = s
				log.Println("get param value:", s, "string value:", v)
			}
			break
		case reflect.Int:
			break
		case reflect.Float64:
			break
		}

		paramValue = append(paramValue, reflect.ValueOf(v))
	}

	return method.Call(paramValue)
}

func ExecNativeReturnOne(methodName string, param []interface{}) reflect.Value {
	native := ExecNative(methodName, param)
	return native[0]
}

func ExecNativeCustContainer(methodName string, param []interface{}) *fyne.Container {
	return ExecNativeReturnOne(methodName, param).Interface().(*fyne.Container)
}
